package com.lam.common.validate.config;

import javax.validation.Validation;
import javax.validation.ValidatorFactory;

import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory;

//@Configuration(proxyBeanMethods = false)
@Configuration
public class ValidatorConfiguration {
 
//    @Bean
//    @ConditionalOnMissingBean(Validator.class)
//    public LocalValidatorFactoryBean validator(AutowireCapableBeanFactory springFactory) {
//        LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
//        MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
//        factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
//        // 快速失败
//        factoryBean.unwrap(type)getValidationPropertyMap().put(BaseHibernateValidatorConfiguration.FAIL_FAST, Boolean.TRUE.toString());
//        return factoryBean;
//    }
    
    @Bean
    public javax.validation.Validator validator1(AutowireCapableBeanFactory springFactory) {
        try (ValidatorFactory factory = Validation.byProvider(HibernateValidator.class)
                .configure().failFast(true)// 快速失败
                // 解决 SpringBoot 依赖注入问题
                .constraintValidatorFactory(new SpringConstraintValidatorFactory(springFactory))
                .buildValidatorFactory()) {
            return factory.getValidator();
        }
    }
 
}
